package com.bizhibihui.ordermeal.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bizhibihui.ordermeal.entity.BizOrder;
import com.bizhibihui.ordermeal.entity.BizOrderFood;
import com.bizhibihui.ordermeal.entity.User;
import com.bizhibihui.ordermeal.service.BizOrderFoodService;
import com.bizhibihui.ordermeal.service.BizOrderService;
import com.bizhibihui.ordermeal.util.CoreConst;
import com.bizhibihui.ordermeal.util.Pagination;
import com.bizhibihui.ordermeal.util.ResultUtil;
import com.bizhibihui.ordermeal.vo.OrderVo;
import com.bizhibihui.ordermeal.vo.base.PageResultVo;
import com.bizhibihui.ordermeal.vo.base.ResponseVo;
import lombok.AllArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @description: 订单管理
 */
@Controller
@RequestMapping("order")
@AllArgsConstructor
public class OrderController {

    private final BizOrderService bizOrderService;

    private final BizOrderFoodService bizOrderFoodService;

    @PostMapping("list")
    @ResponseBody
    public PageResultVo list(BizOrder bizOrder, Integer pageNumber, Integer pageSize) {
        IPage<BizOrder> page = new Pagination<>(pageNumber, pageSize);
        List<BizOrder> orderList = bizOrderService.findByCondition(page, bizOrder);
        return ResultUtil.table(orderList, page.getTotal());
    }

    @PostMapping("/delete")
    @ResponseBody
    public ResponseVo delete(Integer id) {
        int i = bizOrderService.delete(id);
        if (i > 0) {
            return ResultUtil.success("删除订单成功");
        } else {
            return ResultUtil.error("删除订单失败");
        }

    }

    /**
     * 创建订单
     * @return
     */
    @PostMapping("/create")
    @ResponseBody
    public ResponseVo create(@RequestBody OrderVo orderVo){
        if (orderVo.getAddress()==null){
            return ResultUtil.error("请选择配送地址");
        }
        if (orderVo.getOrderInfo()==null || orderVo.getOrderInfo().isEmpty()){
            return ResultUtil.error("请先选择美食");
        }
        boolean result = bizOrderService.create(orderVo);
        if (result){
            return ResultUtil.success("创建订单成功");
        }else{
            return ResultUtil.error("创建订单失败");
        }

    }

    @GetMapping("/edit")
    public String editOrder(Model model, Integer id) {
        BizOrder order = bizOrderService.getById(id);
        model.addAttribute("order",order);
        List<BizOrderFood> foodList = bizOrderFoodService.findByOrder(order.getId());
        model.addAttribute("foodList",foodList);
        return CoreConst.ADMIN_PREFIX + "order/detail";
    }

    @PostMapping("/complete")
    @ResponseBody
    public ResponseVo complete(Integer id){
        int result = bizOrderService.complete(id);
        if (result>0){
            return ResultUtil.success("处理成功");
        }else{
            return ResultUtil.error("处理失败");
        }
    }


    @PostMapping("/batch/complete")
    @ResponseBody
    public ResponseVo completeBatch(@RequestParam("ids[]") Integer[] ids) {
        int i = bizOrderService.completeBatch(ids);
        if (i > 0) {
            return ResultUtil.success("处理成功");
        } else {
            return ResultUtil.error("处理失败");
        }
    }

    @GetMapping("myOrder")
    public String myOrder(Model model){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("user",user);
        BizOrder bizOrder = new BizOrder();
        bizOrder.setUserId(user.getUserId());
        IPage<BizOrder> page = new Pagination<>(1, 10);
        List<BizOrder> orderList = bizOrderService.getMyOrder(page, bizOrder);
        model.addAttribute("orderList",orderList);
        model.addAttribute("pageUrl", "/order/myOrderPage");

        return "/front/order/list";
    }

    @RequestMapping("/myOrderPage/{pageNumber}")
    public String index(@PathVariable("pageNumber") Integer pageNumber, Model model,BizOrder bizOrder) {
        IPage<BizOrder> page = new Pagination<>(pageNumber, 10);
        List<BizOrder> orderList = bizOrderService.getMyOrder(page, bizOrder);
        model.addAttribute("orderList",orderList);
        model.addAttribute("pageUrl", "/order/myOrderPage");
        if (SecurityUtils.getSubject().isAuthenticated()) {
            User user = (User) SecurityUtils.getSubject().getPrincipal();
            model.addAttribute("user", user);
        }
        return "/front/order/list";
    }


}